package com.hyh.admin.config.websocket;

import org.springframework.boot.web.servlet.ServletContextInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;

/**
 * WebSocket配置
 * @author hyh
 */
@Configuration
@EnableWebSocket
public class WebSocketConfig implements ServletContextInitializer {
    /*
     *  ServerEndpointExporter 作用
     *  这个Bean会自动注册使用@ServerEndpoint注解声明的Websocket endpoint
     */
    @Bean
    public ServerEndpointExporter serverEndpointExporter() {
        return new ServerEndpointExporter();
    }

    /*
     * 解除websocket对数据大小的限制
     * @param servletContext Servlet上下文
     *
     */
    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        // 解除websocket对数据大小的限制
        servletContext.setInitParameter("org.apache.tomcat.websocket.textBufferSize","10240000");
        servletContext.setInitParameter("org.apache.tomcat.websocket.binaryBufferSize","10240000");
    }
}
